package top.tangyh.lamp.care.common;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import top.tangyh.lamp.care.entity.BObsvCfg;
import top.tangyh.lamp.care.entity.BObsvPtCfg;
import top.tangyh.lamp.care.vo.result.ObsvCfgDto;
import top.tangyh.lamp.care.vo.result.ObsvPtCfgDto;
import top.tangyh.lamp.common.constant.ObsvConstant;

public class ObsrvCfgValUnil {
	public static <T> T doCfgVal(T bean) {
		if (!((bean instanceof BObsvCfg) || (bean instanceof BObsvPtCfg) ||
				(bean instanceof ObsvCfgDto) || (bean instanceof ObsvPtCfgDto))) {
			return bean;
		}
		String dataType = BeanUtil.getProperty(bean, "dataType");
		String val = BeanUtil.getProperty(bean, "refLower");
		BeanUtil.setProperty(bean,"refLower",getVaL(dataType,val));
		val = BeanUtil.getProperty(bean, "refUpper");
		BeanUtil.setProperty(bean,"refUpper",getVaL(dataType,val));
		return bean;
	}

	public static String getVaL(String dataType, String val) {
		String rst = val;
		if (StrUtil.isBlank(dataType) || StrUtil.isBlank(val)) {
			return rst;
		}

		if (ObsvConstant.TYPE_INT.equals(dataType)) {
			rst = NumberUtil.roundStr(Convert.toDouble(val), 0);
		} else if (ObsvConstant.TYPE_FLOAT.equals(dataType)) {
			rst = NumberUtil.roundStr(Convert.toDouble(val), 2);
		}
		return rst;
	}
}